package com.icinfo.cloud.provider.punish.common.nonlitigation.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.Consts;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.time.LocalDateTime;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

/**
 * @Desc 描述
 * @Author mahan
 * @Date 2023/1/13
 */
@Slf4j
public class NonLitigationUtil {


	/**
	 * 非诉执行接口转到IRS平台访问
	 * @param url
	 * @param token
	 * @param paraMap
	 * @return
	 * @throws Exception
	 */
	public static String doIRSHttpPostWithToken(String url, String token, Map<String, Object> paraMap) throws Exception {
		CloseableHttpClient httpClient = HttpClients.createDefault();
		//访问接口
		HttpPost post = new HttpPost(url);
		try {
			setHeaders(post, url);
			post.addHeader("Authorization", token);
			// 请求类型：application/json
			StringEntity entity = new StringEntity(
					new ObjectMapper().writeValueAsString(paraMap),
					Consts.UTF_8);
			entity.setContentEncoding("UTF-8");
			entity.setContentType(ContentType.APPLICATION_JSON
					.getMimeType());
			post.setEntity(entity);
			String msgRet = EntityUtils.toString(httpClient.execute(post).getEntity(), "UTF-8");
			log.info("Non-litigation Execute return msg:{}" + msgRet);
			return msgRet;
		} catch (Exception e) {
			throw new Exception(e.getMessage());
		} finally {
			httpClient.close();
		}
	}

	/**
	 * 设置请求头
	 * @param post
	 * @param url
	 */
	private static void setHeaders(HttpPost post, String url){
		Map<String, String> headers = HmacAuthUtil.generateHeader(url, "post");
		post.addHeader("X-BG-HMAC-SIGNATURE", headers.get("X-BG-HMAC-SIGNATURE"));
		post.addHeader("X-BG-HMAC-ALGORITHM", headers.get("X-BG-HMAC-ALGORITHM"));
		post.addHeader("X-BG-HMAC-ACCESS-KEY", headers.get("X-BG-HMAC-ACCESS-KEY"));
		post.addHeader("X-BG-DATE-TIME", headers.get("X-BG-DATE-TIME"));
	}


	public static void main(String[] args) throws Exception{

		Map<String, Object> paramMap = new HashMap<>();
		//接入系统编号
		paramMap.put("access_xtbh", "53");
		//访问接入系统服务的终端ip
		paramMap.put("ip", "220.191.249.136");
		//访问接入系统的登陆人姓名
		paramMap.put("name", "");
		//访问接入系统的登陆人身份证号
		paramMap.put("idNumber", "");
		//访问接入系统用途
		paramMap.put("use", "行政非诉智审网上立案");
		//接入系统名称
		paramMap.put("csxtmc", "统一处罚办案系统");
		//接入系统调用接口时间
		paramMap.put("time", LocalDateTime.now());

		String username = "lhxzhzhx";
		String password = "728605d6";
		String basicToken = "Basic " + Base64.getUrlEncoder().encodeToString((username + ":" + password).getBytes());
		System.out.println(basicToken);

		String msg = doIRSHttpPostWithToken("https://bcdsg.zj.gov.cn:8443/restapi/prod/IC33000020220309000003/zjfyw/auth/dw-api/bauth/token", basicToken, paramMap);
		System.out.println("msg：" + msg);
	}
}
